package com.zzjson.se;

import com.zzjson.se.control.EncodedControl;

import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2020 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.se</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2020年04月08日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class ResourceBundleDemo {

	/**
	 * package（目录） + resource 名称
	 */
	public static final String BUNDLE_NAME = "demo";

	public static void main(String[] args) {

		extendControl();

		withoutEncode();

		// -Dfile.encoding=UTF-8 无效
		simpleEncode();


	}


	public static void simpleEncode() {
		Locale.setDefault(Locale.SIMPLIFIED_CHINESE);
		ResourceBundle resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);

		//因为当前没有使用unicode来写，默认是iso_8859_1,所以转化，避免乱码
		System.out.println(new String(resourceBundle.getString("name").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
	}


	public static void withoutEncode() {
		ResourceBundle resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
		String name = resourceBundle.getString("name");
		System.out.println(name);
	}

	/**
	 * 基于 Java 1.6
	 * 显示地传递 EncodedControl
	 */
	public static void extendControl() {
		Locale.setDefault(Locale.SIMPLIFIED_CHINESE);
		ResourceBundle resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, new EncodedControl("utf8"));
		System.out.println("resourceBundle.name : " + resourceBundle.getString("name"));
	}
}
